package cn.skyclass.controls
{
    import flash.external.ExternalInterface;
    import flash.geom.Point;
    import flash.events.Event;
	
	/**
	 * flash 中的 iframe 实现，在 flash 内部模拟显示外部网页，类似 iframe 效果
	 * 
	 * 使用此组件需要在包含此 flash 的 html 页面中作些改动：(参考 iframe-demo.txt)
	 * 可以直接使用 iframe.template.html 替换原来的 **.template.html
	 * 1、在 html 页面中引用 iframe.js 文件。
	 * 2、在 flash 标签部分添加 wmode="opaque" 属性。
	 */
	[Bindable]
	public class IFrame extends OuterWindow
	{
		/**
		 * 标识 html 页面中对应的 div 的 id ，
		 * IFrame 将使用此 div 来加载需要显示的网页
		 */		
		public var iframeId:String = "iframeFlex";
				
        /**
         * Move iframe through ExternalInterface.  The location is determined using localToGlobal()
         * on a Point in the Canvas.
         **/
        protected function moveIFrame():void{
            var localPt:Point = new Point(0, 0);
            var globalPt:Point = this.localToGlobal(localPt);
            ExternalInterface.call("moveIFrame", this.iframeId, globalPt.x, globalPt.y, this.width, this.height);
        }
		
        private var _source: String;
        public function get source():String{
            return _source;
        }
        public function set source(source:String):void{
            if (source){
                if (! ExternalInterface.available){
                	var msg:String = "ExternalInterface is not available in this container.";
                	msg += " Internet Explorer ActiveX, Firefox, Mozilla 1.7.5 and greater,";
                	msg += " or other browsers that support NPRuntime are required.";
                    throw new Error(msg); 
                }
                _source = source;
                ExternalInterface.call("loadIFrame", this.iframeId, source);                
                this.visible = true;
                this.setVisibility(true);
                this.moveIFrame();
            }
        }

		override public function get visible():Boolean{
			return super.visible;
		}
        override public function set visible(visible:Boolean):void{
            super.visible = visible;
            ExternalInterface.call("setIFrameVisibility", this.iframeId, visible);            
        }
        
		public function setVisibility(visible:Boolean):void{
			super.visible = visible;
            ExternalInterface.call("setIFrameVisibility", this.iframeId, visible);			
		}
		        
        override protected function moveOuterWindow(e:Event=null):void{
			this.moveIFrame();
		}
		
	}
}